﻿/*var prefixUrl = "http://140.143.8.95:8088/djk/";
var imageUrl = "http://140.143.8.95:8088/djkWeb/djkImg/";*/
var prefixUrl = "http://localhost:8080/plaque/";
var imageUrl = "http://localhost:8080/plaque/djkImg/";
var shopId = "f4f9f4ff8f6a4ce7b67ce12daed977d6";

var commonId;
var webImg;
var phoneImg;
var commonDetail;

var ue = UE.getEditor("content");

var messageHtml = '<thead>'
    +'<tr>'
    +'<th><input type="checkbox" class="checkbox"></th>'
    +'<th>文章标题</th>'
    +'<th>文章类型</th>'
    +'<th>时间</th>'
    +'<th>推荐</th>'
    +'<th>操作</th>'
    +'</tr>'
    +'</thead>'
    +'<tbody>';

var baseHtml = '<thead>'
    +'<tr>'
    +'<th><input type="checkbox" class="checkbox"></th>'
    +'<th>基地名称</th>'
    +'<th>基地类型</th>'
    +'<th>时间</th>'
    +'<th>推荐</th>'
    +'<th>操作</th>'
    +'</tr>'
    +'</thead>'
    +'<tbody>';

var mountainHtml = '<thead>'
    +'<tr>'
    +'<th><input type="checkbox" class="checkbox"></th>'
    +'<th>山庄名称</th>'
    +'<th>山庄类型</th>'
    +'<th>时间</th>'
    +'<th>推荐</th>'
    +'<th>操作</th>'
    +'</tr>'
    +'</thead>'
    +'<tbody>';

var vacationHtml = '<thead>'
    +'<tr>'
    +'<th width="3%"><input type="checkbox" class="checkbox"></th>'
    +'<th width="8%">仓库统计标题</th>'
    +'<th width="20%">选择类型</th>'
    +'<th width="9%">出发日期</th>'
    +'<th width="18%">活动简介</th>'
    +'<th width="6%">贡献值</th>'
    +'<th width="6%">操作</th>'
    +'</tr>'
    +'</thead>'
    +'<tbody>';

function setCommonId(id){
    commonId = id;

    $.ajax({
        url: prefixUrl + "common/queryDetail",
        type: "post",
        data: JSON.stringify({id:id}),
        contentType: "application/json;",
        success: function (data) {
            commonDetail = data.data;
            var detail = data.data;
            $("#name").val(detail.name);
            $("#source").val(detail.source);
            $("#contribution").val(detail.contribution);
            $("#classifyList").val(detail.classifyId);
            //$("#content").val(detail.content);
            ue.setContent(detail.content);
            $("#webImg").val(detail.webImg);
            $("#phoneImg").val(detail.phoneImg);
        }
    })
}

function savePhoneImg(){
    var formData = new FormData($( "#uploadPic" )[0]);
    $.ajax({
        type: "POST",
        url: prefixUrl + "image/upload",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            phoneImg =  data;
            $("#phoneImg").attr("src",imageUrl + data);
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function saveWebImg(){
    var formData = new FormData($( "#uploadPic" )[0]);
    $.ajax({
        type: "POST",
        url: prefixUrl + "image/upload",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            webImg =  data;
            $("#webImg").attr("src",imageUrl + data);
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function saveLunBoPic(formData){
    $.ajax({
        type: "POST",
        url: prefixUrl + "image/upload",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            lunboImgesUrl =  lunboImgesUrl + imageUrl + data  + "," ;
            console.log(lunboImgesUrl);
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function saveCommon(type){
    var saveData = {};
    if($("#name").val() == null || $("#name").val() == ""){
        alert("名称不可为空");
        return;
    }
    if($("#source").val() == null || $("#source").val() == ""){
        alert("来源不可为空");
        return;
    }
    if( type == 4 && ($("#contribution").val() == null || $("#contribution").val() == "")){
        alert("贡献值不可为空");
        return;
    }
    saveData = {
        id:commonId,
        name:$("#name").val(),
        source:$("#source").val(),
        contribution:$("#contribution").val(),
        classifyId:$("#classifyList").val(),
        classifyName:$("#classifyList").find("option:selected").text(),
        content:ue.getContent(),
        phoneImg:phoneImg,
        webImg:webImg,
        actType:type
    };
    $.ajax({
        type: "POST",
        url: prefixUrl + "common/add",
        data: JSON.stringify(saveData),
        contentType: "application/json;",
        success: function (data) {
            alert(data.msg);
            if(type == 1){
                getData(type,$("#articleType").val());
            }else{
                getData(type);
            }
            modalDisplay();
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function updateCommon(type){
    if($("#name").val() == null || $("#name").val() == ""){
        alert("名称不可为空");
        return;
    }
    if($("#source").val() == null || $("#source").val() == ""){
        alert("来源不可为空");
        return;
    }
    if( type == 4 && ($("#contribution").val() == null || $("#contribution").val() == "")){
        alert("贡献值不可为空");
        return;
    }
    var updateData = {};
    if(type == 1){
        updateData = {
            id:commonId,
            name:$("#name").val(),
            source:$("#source").val(),
            classifyId:$("#classifyList").val(),
            classifyName:$("#classifyList").find("option:selected").text(),
            content:$("#content").val(),
            phoneImg:phoneImg,
            webImg:webImg
        };
    }else{
        updateData = {
            id:commonId,
            name:$("#name").val(),
            contribution:$("#contribution").val(),
            source:commonDetail.source,
            classifyId:$("#classifyList").val(),
            classifyName:$("#classifyList").find("option:selected").text(),
            content:$("#content").val(),
            phoneImg:commonDetail.phoneImg,
            webImg:commonDetail.webImg
        };
    }

    $.ajax({
        type: "POST",
        url: prefixUrl + "common/update",
        data: JSON.stringify(updateData),
        contentType: "application/json;",
        success: function (data) {
            alert(data.msg);
            if(type == 1){
                getData(type,$("#articleType").val());
            }else{
                getData(type);
            }
            modalDisplay();
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function deleteCommon(id,type){
    $.ajax({
        type: "POST",
        url: prefixUrl + "common/delete",
        data: JSON.stringify({id:id}),
        contentType: "application/json;",
        success: function (data) {
            alert(data.msg);
            if(type == 1){
                getData(type,$("#articleType").val());
            }else{
                getData(type);
            }
        },
        error: function(data) {
            alert("error:"+data.responseText);
        }
    });
}

function getData(type,classifyId){
    var reqData = {};
    if(type == 1){
        reqData = {"actType":type,classifyId:classifyId};
    }else {
        reqData = {"actType":type};
    }
    $.ajax({
        url: prefixUrl + "common/queryList",
        type: "post",
        data: JSON.stringify(reqData),
        contentType: "application/json;",
        success: function (data) {
            var commonHtml = ""
            if(type == 1){
                commonHtml = messageHtml;
            }else if(type == 2){
                commonHtml = baseHtml;
            }else if(type == 3){
                commonHtml = mountainHtml;
            }else if(type == 4){
                commonHtml = vacationHtml;
            }
            var list = data.data;
            for(var i=0; i<list.length; i++){
                commonHtml = commonHtml
                    +'<tr>'
                    +'<td><input type="checkbox" class="checkbox"></td>'
                    +'<td>'
                    +list[i].name
                    +'</td>'
                    +'<td>'
                    +list[i].classifyName
                    +'</td>'
                    +'<td>'
                    +new Date(list[i].createTime).toLocaleDateString()
                    +'</td>'
                    +'<td>暂无</td>';
                    if(type == 4){
                        commonHtml = commonHtml + '<td>' + list[i].contribution + '</td>';
                    }
                    commonHtml = commonHtml
                    +'<td>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=setCommonId("'
                    + list[i].id
                    +'")><i class="fa fa-pencil"></i></button>'
                    +'<button type="button" class="btn btn-info" onclick=deleteCommon("'
                    +list[i].id
                    +'",'
                    +list[i].actType
                    +')><i class="fa fa-bitbucket-square"></i></button>'
                    +'</td>'
                    +'</tr>';
            }
            commonHtml = commonHtml + +'</tbody>';
            $("#commonTbale").html(commonHtml);
        }
    })

}

function commodityUpdateHide(){
    $("#updateCommodity").$element.on($.proxy(this.hide, this));
}

function modalDisplay(){
    $(".modal-backdrop").css("display","none");
    $(".modal").css("display","none");
}

function goToLogin(data){
    if(data.code == "1001"){
        alert(data.msg);
        window.location.href = "login.html";
    }
}